﻿package core.ui.dialogs
{
    import core.enum.*;
    import core.event.*;
    import core.image.*;
    import core.image.decoder.*;
    import core.image.psd.*;
    import core.ui.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class OpenImageDialog extends Object
    {
        private var ed:Tuyitu;
        private var loader:URLLoader;
        private var aslayer:Boolean;
        private var file:FileReference;
        private var files:FileReferenceList;
        private var uploadURL:URLRequest;
        private var ld:LoadDialog;
        private var filename:String = "";
        private var filetype:String = "";

        public function OpenImageDialog(param1:Tuyitu, param2:String = "", param3:String = "", param4:Boolean = false, param5:Boolean = false)
        {
            this.ed = param1;
            this.aslayer = param5;
            this.filename = param3 != "" ? (param3) : (U.RM("rm_untitled"));
            if (param2 == "")
            {
                this.ShowBrowseDialog();
            }
            else
            {
                this.LoadImage(param4 || U.IsParamTrue("credentials") ? (param2) : ("http://website.m5cn.com/proxy.php?url=" + escape(param2)));
            }
            return;
        }// end function

        private function ShowBrowseDialog() : void
        {
            this.files = new FileReferenceList();
            this.files.addEventListener(Event.SELECT, this.NextFileInLine);
            this.files.addEventListener(Event.CANCEL, this.ed.CheckToShowSplash);
            this.files.browse(new Array(new FileFilter(U.RM("rm_images") + " (*.jpg, *.jpeg, *.gif, *.png, *.bmp, *.psd, *.tud, *.TUD)", "*.JPG;*.jpg;*.JPEG;*.jpeg;*.GIF;*.gif;*.PNG;*.png;*.BMP;*.bmp;*.psd;*.PSD;*.tud;*.TUD;")));
            return;
        }// end function

        private function NextFileInLine(... args) : void
        {
            args = new activation;
            var _args:* = args;
            var args:* = ;
            if (this.files.fileList.length > 0)
            {
                this.file = this.files.fileList.shift();
                this.file.addEventListener(Event.COMPLETE, function ()
            {
                DecodeByteArray(file.data);
                return;
            }// end function
            );
                this.filename = this.file.name;
                this.filetype = this.filename.indexOf(".") != -1 ? (this.filename.substr((this.filename.lastIndexOf(".") + 1)).toLowerCase()) : ("jpg");
                this.file.load();
            }
            return;
        }// end function

        private function LoadImage(Boolean:String) : void
        {
            this.ld = new LoadDialog(this.ed, U.RM("rm_loading_image"));
            this.ld.addEventListener(DialogEvent.CANCEL, this.LoadCancel);
            this.filetype = Boolean.indexOf(".") != -1 ? (Boolean.substr((Boolean.lastIndexOf(".") + 1)).toLowerCase()) : ("jpg");
            this.loader = new URLLoader();
            this.loader.addEventListener(ProgressEvent.PROGRESS, this.LoaderProgress);
            this.loader.addEventListener(IOErrorEvent.IO_ERROR, this.ErrorOccurred);
            this.loader.addEventListener(Event.COMPLETE, this.LoaderComplete);
            this.loader.dataFormat = "binary";
            this.loader.load(new URLRequest(Boolean));
            return;
        }// end function

        private function LoadCancel(... args) : void
        {
            if (this.loader != null)
            {
                this.loader.close();
            }
            this.ed.CheckToShowSplash();
            return;
        }// end function

        private function LoaderProgress(event:ProgressEvent) : void
        {
            this.ld.pgb.Set(event.bytesLoaded <= 0 || event.bytesTotal <= 0 ? (0) : (event.bytesLoaded / event.bytesTotal));
            return;
        }// end function

        function LoaderComplete(event:Event) : void
        {
            if (this.ld != null)
            {
                this.ld.Close();
                this.ld = null;
            }
            this.DecodeByteArray(this.loader.data);
            return;
        }// end function

        private function DecodeByteArray(Boolean:ByteArray) : void
        {
            var ldr:*;
            var _arg1:* = Boolean;
            ldr;
            var data:* = _arg1;
            if (this.filetype == "tud" || this.filetype == "psd" || this.filetype == "bmp")
            {
                this.CreateCanvas(data);
            }
            else
            {
                ldr = new Loader();
                ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, function (event:Event)
            {
                var _loc_2:Reform = null;
                if (ldr.content.width > 4000 || ldr.content.height > 4000)
                {
                    _loc_2 = new Reform();
                    CreateCanvas(_loc_2.MaxSize(event.target.content.bitmapData, 4000, 4000));
                }
                else
                {
                    CreateCanvas(event.target.content.bitmapData);
                }
                return;
            }// end function
            );
                ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.ErrorOccurred);
                ldr.loadBytes(data);
            }
            return;
        }// end function

        private function CreateCanvas(Boolean) : void
        {
            var _arg1:* = Boolean;
            var document:*;
            var tuddecoder:*;
            var psdfile:*;
            var bmpdecoder:*;
            var src:*;
            var bm:*;
            var i:*;
            var layer:*;
            var data:* = _arg1;
            try
            {
                switch(this.filetype)
                {
                    case "tud":
                    {
                        tuddecoder = new TUDDecoder();
                        document = tuddecoder.decode(data, this.filename);
                        break;
                    }
                    case "psd":
                    {
                        psdfile = PSDFile.Load(data);
                        document = new Document(this.filename, psdfile.header.Columns, psdfile.header.Rows);
                        i;
                        while (i < psdfile.Layers.length)
                        {
                            
                            layer = new Layer(LayerType.Bitmap, psdfile.Layers[i].Name, psdfile.Layers[i].Dimensions.topLeft, !Boolean(psdfile.Layers[i].Flags & 3) || i == 0, psdfile.Layers[i].Opacity / 255);
                            layer.AddBitmap(ImageHelper.DecodeLayer(psdfile, psdfile.Layers[i]));
                            document.AddLayer(layer);
                            i = (i + 1);
                        }
                        break;
                    }
                    case "bmp":
                    {
                        bmpdecoder = new BMPDecoder();
                        src = bmpdecoder.decode(data);
                        bm = new BitmapData(src.width, src.height, true, 16777215);
                        bm.copyPixels(src, src.rect, src.rect.topLeft);
                        if (!this.aslayer)
                        {
                            document = new Document(this.filename, bm.width, bm.height);
                            document.AddBitmapLayer(bm);
                        }
                        else
                        {
                            this.ed.cm.Paste(bm);
                        }
                        break;
                    }
                    default:
                    {
                        src = data as BitmapData;
                        bm = new BitmapData(src.width, src.height, true, 16777215);
                        bm.copyPixels(src, src.rect, src.rect.topLeft);
                        if (!this.aslayer)
                        {
                            document = new Document(this.filename, bm.width, bm.height);
                            document.AddBitmapLayer(bm);
                        }
                        else
                        {
                            this.ed.cm.Paste(bm);
                        }
                        break;
                    }
                }
            }
            catch (error:Error)
            {
                ErrorOccurred(error);
                return;
            }
            if (document != null)
            {
                this.ed.AddCanvas(new Canvas(this.ed, document));
                this.ed.canvas.history.Did(new HistoryEntry(Action.Open));
                if (this.files != null && this.files.fileList.length > 0)
                {
                    setTimeout(this.NextFileInLine, 750);
                }
            }
            else if (!this.aslayer)
            {
                this.ErrorOccurred(null);
            }
            return;
        }// end function

        private function ErrorOccurred(Boolean) : void
        {
            if (this.ld != null)
            {
                this.ld.Close();
                this.ld = null;
            }
            new AlertDialog(this.ed, "rm_error");
            return;
        }// end function

    }
}
